<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />    
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>

    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <div class="header-title">
            分销订单
        </div>
        <div class="header-r aui-pull-right">
        </div>
    </header>
    <div style="height:4rem;"></div>

    <div class="distribution-withdraw-t">
        累计佣金：+235.60元
    </div>
    <ul class="distribution-withdraw-nav">
        <li id="all" onclick="changeClass('all');">所有</li>
        <li id="hasCancel" onclick="changeClass('hasCancel');">已取消</li>
        <li id="waitComplete" onclick="changeClass('waitComplete');">待完成</li>
        <li id="hasComplete" onclick="changeClass('hasComplete');">已完成</li>
    </ul>
    <div id="list">

    </div>

</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="withdraw-details">
        <div class="withdraw-details-t">
            <img src="./images/gril.jpg">
            {{=value.order_user_nickname}}
        </div>
        <div class="withdraw-details-m">
        <div class="withdraw-details-m-l" onclick="goodsDetails({{=value.order_goods_id}});">
                <img src="{{=value.goods_image}}">
                <dl>
                    <dt>{{=value.order_title}}</dt>
                    <dd>x{{=value.goods_qty}}</dd>
                </dl>
            </div>
            <div class="withdraw-details-m-r">
                预计
                <p>+{{=value.sale_money}}</p>
            </div>
        </div>
        <div class="withdraw-details-d">
            分销等级：{{=value.level}}级
            <p>订单编号：{{=value.order_no}}</p>
            <p>下单时间：{{=value.create_time}}</p>
        </div>
        <div class="withdraw-details-d-d">
            预计佣金：<span>+{{=value.sale_money}}</span>
        </div>
    </div>
    {{~}}
</script>
<script type="text/javascript">

    apiready = function(){
        $('#all').addClass('active');
        globalType = '';
        loadData(globalType);
    }
    
    function loadData(type){
        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/sale/order',
            method: 'post',
            data: {
                values: {
                    type: type,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );

                // 时间戳转换为时间
                var data = ret.result.data;
                for (var i = 0; i < data.length; i++) {
                 data[i].create_time = toDate(data[i].create_time);   
             }

             templateFun(data);
         } else {

                // alert( JSON.stringify( err ) );
                // if (err.body.message) {
                //     api.toast({
                //         msg: err.body.message,
                //         duration: 2000,
                //         location: 'bottom'
                //     });
                // }
                return false;
            }
        });
    }

        // 字符串转时间
        function toDate(timestamp){

            //时间戳为10位需*1000，时间戳为13位的话不需乘1000
            timestamp = parseInt(timestamp);
            var date = new Date(timestamp * 1000);
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            var D = date.getDate() + ' ';
            var h = date.getHours() + ':';
            var m = date.getMinutes() + ':';
            var s = date.getSeconds();
            return Y+M+D+h+m+s;
        }

    // 模板赋值函数
    function templateFun(data){
        if (data.length) {
          var template = $api.byId('template');
          var dotFn = doT.template(template.innerHTML);
          var html = dotFn(data);
          var list = $api.byId('list');
          $api.html(list, html);
      }else{
          var list = $api.byId('list');
          var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:1rem;">暂无内容</h2>';
          list.innerHTML = html;
      }
  }

    // 查看商品详情
    function goodsDetails(goodsid){
        var delay = 0;
        if(api.systemType != 'ios'){
            delay = 300;
        }
        api.openWin({
            name: 'goodsdeatils',
            url: './goods_details.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
                type:"page"
            },
            pageParam:{
                id: goodsid,
            }
        });
    }

    // 点击切换类别
    function changeClass(type){
        $('li').removeClass('active');
        $('#'+type).addClass('active');

        if (type == 'all') {
            globalType = '';
        }else if(type == 'hasCancel'){
            globalType = 1;
        }else if(type == 'waitComplete'){
            globalType = 2;
        }else if(type == 'hasComplete'){
            globalType = 3;
        }
        loadData(globalType);
    }

    function withdrawdetails(){
        var delay = 0;
        if(api.systemType != 'ios'){
            delay = 300;
        }
        api.openWin({
            name: 'withdrawdetails',
            url: './distribution-withdraw-details.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
                type:"page"
            }
        });
    }
    
    function closeWin(){
        api.closeWin({
        });
    }
</script>
</html>